package cn.nexd.utils.framework.tools;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by wangxu on 17/3/20.
 */

public class ParamBus {
    private final Map<Class<?>, Object> params = new ConcurrentHashMap<>();
    private final Map<String, Object> paramKeyValue = new ConcurrentHashMap<>();
    private static final ParamBus ourInstance = new ParamBus();

    public static ParamBus getInstance() {
        return ourInstance;
    }

    private ParamBus() {
    }

    public void put(Object param) {
        params.put(param.getClass(), param);
    }

    public <T> T get(Class<T> clazz) {
        return clazz.cast(params.get(clazz));
    }

    public void put(String key, Object value) {
        paramKeyValue.put(key, value);
    }

    public Object get(String key) {
        return paramKeyValue.get(key);
    }


    public void remove(Class clazz) {
        synchronized (params) {
            params.remove(clazz);
        }
    }

    public void remove(String key) {
        synchronized (paramKeyValue) {
            paramKeyValue.remove(key);
        }
    }

    public void removeAll() {
        synchronized (params) {
            params.clear();
            paramKeyValue.clear();
        }
    }
}
